package com.qf.dao.impl;

import com.qf.dao.ProfileDao;
import com.qf.entity.Profile;
import com.qf.utils.DruidUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.awt.print.Book;
import java.sql.SQLException;
import java.util.List;

public class ProfileDaoImpl implements ProfileDao {
    @Override
    public long selectCount() {
        QueryRunner qr=new QueryRunner(DruidUtils.getDataSource());
        try {
            return qr.query("select count(*) from Profile", new ScalarHandler<>());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Profile> selectByPage(int pn, int ps) {
        QueryRunner qr=new QueryRunner(DruidUtils.getDataSource());
        Object[] params={(pn-1)*ps,ps};
        try {
            return qr.query("select id,name,birthday,gender,career,address,mobile,picture from Profile order by id limit ?,?", new BeanListHandler<>(Profile.class),params);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public List<Profile> selectAll() {
        QueryRunner qr=new QueryRunner(DruidUtils.getDataSource());
        try {
            return qr.query("select id,name,birthday,gender,career,address,mobile,picture from Profile", new BeanListHandler<>(Profile.class));
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
